package com.jiutong.meipengdai.http.present;

import android.content.Context;

import com.jiutong.meipengdai.entity.EvaluateRespond;
import com.jiutong.meipengdai.entity.HouseDetailBean;
import com.jiutong.meipengdai.entity.LoanDetailBean;
import com.jiutong.meipengdai.entity.MyloanBean;
import com.jiutong.meipengdai.http.model.interfaces.LoanRelaBize;
import com.jiutong.meipengdai.http.model.requests.LoanRelaRequest;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;

import java.util.HashMap;

/**
 * 贷款相关请求
 * Created by suxi on 2017/5/31.
 */

public class LoanRelaPresent {

    private final Context mContext;
    private final LoanRelaBize mBaseBiz;
    private final LoanRelaRequest request;

    public LoanRelaPresent(Context context, LoanRelaBize baseBiz) {
        this.mContext = context;
        this.mBaseBiz = baseBiz;
        request = new LoanRelaRequest();
    }

    /**
     * 自动认证
     */
    public void autoAuth() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<EvaluateRespond>() {
            @Override
            public void onNext(EvaluateRespond subjects) {
                mBaseBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mBaseBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("areaName", mBaseBiz.getAreaName());
        fieldsMap.put("locaName", mBaseBiz.getLocaName());
        fieldsMap.put("orientate", mBaseBiz.getOrientate());
        fieldsMap.put("area", mBaseBiz.getArea());
        fieldsMap.put("inFloor", mBaseBiz.getInFloor());
        fieldsMap.put("totalFloor", mBaseBiz.getTotalFloor());
        request.autoAuth(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 贷款记录
     */
    public void loanRecord(String loanId) {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<MyloanBean>() {
            @Override
            public void onNext(MyloanBean subjects) {
                mBaseBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mBaseBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("loanId", loanId);
        request.loanRecord(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 贷款详情
     */
    public void loanDetail(String loanId) {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<LoanDetailBean>() {
            @Override
            public void onNext(LoanDetailBean o) {
                mBaseBiz.onSuccess(o);
            }

            @Override
            public void onError(Throwable e) {
                mBaseBiz.onError(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("loanId", loanId);
        request.loanDetail(new ProgressSubscriber(onNextListener, mContext), fieldsMap);

    }

    /**
     * 房产详情
     *
     * @param houseId
     */
    public void getHouseDetail(String houseId) {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<HouseDetailBean>() {
            @Override
            public void onNext(HouseDetailBean o) {
                // TODO: 2017/6/21
                mBaseBiz.onSuccess(o);
            }

            @Override
            public void onError(Throwable e) {
                mBaseBiz.onError(e);
            }
        };
        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("houseId", houseId);
        request.getHouseDetail(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     *
     * 还款列表
     */
    public void repayList() {
        SubscriberOnNextListener onNextListener =new SubscriberOnNextListener<LoanDetailBean>() {
            @Override
            public void onNext(LoanDetailBean o) {
                mBaseBiz.onSuccess(o);
            }

            @Override
            public void onError(Throwable e) {
                mBaseBiz.onError(e);
            }
        };
        HashMap<String,Object> fieldsMap=new HashMap<>();
        request.repayList(new ProgressSubscriber(onNextListener,mContext),fieldsMap);

    }
}
